const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    workTypeList:[
      {
        text: '全部',
        value: 0
      },
      {
        text: '服务',
        value: 2
      },
      {
        text: '作品',
        value: 1
      },
    ],
    navHeight: '',
    filterBoxShow:false,
    worksCategoryIndex:1,
    taskCateText:'全部分类',
    worksList:'',
    worksCateList:'',
    sortList: [
      {
        title: '综合排序',
        index: 0
      },
      {
        title: '发布时间',
        index: 1
      },
      {
        title: '成交量',
        index: 2
      },
      {
        title: '好评数',
        index: 3
      }
    ],
    filterActive:"",
    type:'',
    name:'',
    cate_id:'',
    desc:'',
    pageNum: 1,
    searchVal:'',
    loading:false,
    filterInfo:{
      startCash: '',
      endCash: '',
      pattern:0,
      workType:0,
      state:0
    },
  },

  // 点击商城列表
  shopList: function (e) {
    var { id,type } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/taskPage/pages/worksDetail/worksDetail?id=' + id + '&type=' + type,
    })
  },

  worksFilter(e){
    this.setData({
      filterInfo:e.detail,
      pageNum: 1,
      worksList: ''
    })
    this.worksList();
  },

  filterCardClear(){
    const child=this.selectComponent('.filterCard')
    child.clearData()
    let data={
      startCash: '',
      endCash: '',
      pattern:0,
      workType:0,
      state:0
    }
    this.setData({
      filterInfo:data
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    wx.getSystemInfo({
      success(res) {
        _this.setData({
          navHeight: res.statusBarHeight
        })
      }
    })
    if (options.id) {
      this.setData({
        cate_id: options.id,
        taskCateText: options.text,

      })
    }
    // 全部分类
    this.allCate(options);
    // 作品列表
    this.worksList();
  },

  // 分类切换
  taskCategoryTab: function () {
    this.setData({
      cate_id:'',
      pageNum:1,
      worksList:''
    })
    this.worksList();
  },
  // 分类切换
  talentCategoryTab: function (e) {
    var { index } = e.currentTarget.dataset
    this.setData({
      worksCategoryIndex: index
    })
  },
  // 分类列表显示隐藏
  categoryBtn: function () {
    var { filterBoxShow } = this.data
    this.setData({
      filterBoxShow: !filterBoxShow,
      filterType: 0
    })
  },
  // 排序列表显示隐藏
  sortBtn: function () {
    var { filterBoxShow } = this.data
    this.setData({
      filterBoxShow: !filterBoxShow,
      filterType: 1
    })
  },
  // 选择分类
  taskCateType: function (e) {
    var { id, name } = e.detail;
    if(id===0 && name==='全部分类'){
      this.taskCategoryTab()
    }else {
      this.setData({
        taskCateText: name,
        cate_id: id,
        pageNum: 1,
        worksList: ''
      })
      this.worksList();
    }

  },
  // 选择排序
  sortType: function (e) {
    var { index } = e.detail;
    this.setData({
      desc: index,
      pageNum: 1,
      worksList: ''
    })
    this.worksList();
  },
  // 输入框搜索
  searchInp: function (e) {
    console.log(e,'ee------')
    this.filterCardClear()
    this.setData({
      name: e.detail.searchName
    })
    if (e.detail.searchName == '') {
      this.setData({
        pageNum: 1,
        worksList: ''
      })
      this.worksList();
    }
  },
  // 搜索
  searchBtn: function () {
    this.setData({
      pageNum: 1,
      worksList: '',
      cate_id: '',
      desc: '',
      type: '',
    })
    this.worksList();
  },
  //全部分类
  allCate: function (options) {
    var _this = this;
    wx.request({
      url: app.globalData.baseUrl + 'taskCate',
      data: {},
      success(res) {
        console.log(res)
        if (res.data.code == 1000) {
          let all={
            text: "全部分类",
            children:[
              {
                name:'全部分类',
                text: "全部分类",
                id:0,
              }
            ]
          }
          let dataList=res.data.data
          dataList?.forEach(item=>{
            item['text']=item.name
            item['children']=item.children_task
            item['children']?.forEach(res=>{
              res['text']=res.name
            })
          })
          dataList.unshift(all)
          _this.setData({
            worksCateList: dataList
          })
          if (options.index) {
            _this.setData({
              filterActive:options
            })
          }
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 作品列表
  worksList:function(){
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      loading:false
    })
    var _this = this;
    var { pageNum, name, cate_id, desc, type, worksList } = this.data;
    let {startCash,endCash,workType} =this.data.filterInfo
    if(workType!==0) type=workType
    wx.request({
      url: app.globalData.baseUrl + 'commodityLists',
      data: {
        cate_id: cate_id,
        type,
        page: pageNum,
        name: name,
        desc: desc,
        startCash,
        endCash
      },
      success(res) {
        wx.hideLoading()

        console.log(res)
        if (res.data.code == 1000) {
          _this.setData({
            worksList: [...worksList, ...res.data.data.data]
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
        _this.setData({
          loading:_this.data.worksList.length===0
        })
      }
    })
  },
  // 返回按钮
  returnBtn:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  // 作品详情
  worksDetailBtn:function(e){
    var { id,type } = e.currentTarget.dataset
    wx.navigateTo({
      url: '/taskPage/pages/worksDetail/worksDetail?id=' + id + '&type=' + type,
    })
  },
  // 购买作品按钮
  buyBtn:function(e){
    var { id, type, title, cash, name, uid } = e.currentTarget.dataset
    if(type == 1){
      var url = app.globalData.baseUrl + 'user/buyGoods';
      var data = {
        goods_id: id,
        token: app.globalData.token
      }
      wx.showLoading({
        title: '正在生成订单',
      })
      app.wxGetRequest(url, data, (res) => {
        wx.hideLoading()
        console.log(res)
        if (res.code == 1000) {
          wx.navigateTo({
            url: '/taskPage/pages/worksPay/worksPay?id=' + id + '&title=' + title + '&cash=' + cash + '&orderid=' + res.data.order_id + '&code=' + res.data.code,
          })
        }else{
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 2000
          })
        }
      }, (err) => {
        wx.hideLoading()
      })
    }else{
      wx.navigateTo({
        url: '/pages/hireForm/hireForm?userName=' + name + '&uid=' + uid + '&id=' + id + '&type=' + type,
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var { pageNum } = this.data;
    pageNum ++;
    this.setData({
      pageNum
    })
    this.worksList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
